﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneController  {
    //状态的定义
    //设置状态
    //状态处理：1.加载 2.更新
    private ISceneState m_state;
    private bool m_isRunState = false;
    private AsyncOperation m_async;

    public void SetState(ISceneState state)
    {
        //正在设置state中 ，m_isRunStae为0
        m_isRunState = false;
        //通知前一个结束
        if (m_state != null) m_state.StateEnd();
        //更新状态
        m_state = state;
        //开始加载状态
        LoadScene(m_state);
    }


    private void LoadScene(ISceneState state) {
        //场景是否存在
        if (state == null) return;
        //加载新场景
        m_async = SceneManager.LoadSceneAsync(state.SceneName); 
    }

    public void StateUpdate()
    {
        //是否正在加载
        if (!m_async.isDone) return;
        //通知新场景开始
        if (!m_isRunState && m_state!=null)
        {
            m_state.StateStart();
           // Debug.Log("start");
            m_isRunState = true;
        }
        if (m_state != null)
        {
            //Debug.Log("update");
            m_state.StateUpdate();
        }
    }
}
